<template>
  <view>
    <!-- 首页 -->
    <homePage v-if="PageCur == 'home'">
      <home
        :categories="categories"
        :sysCategories="sysCategories"
        :permissionCategories="permissionCategories"
        :recordCategories="recordCategories"
        :scrollCategories="scrollCategories"
      ></home>
    </homePage>
  </view>
</template>

<script>
// 首页
import Home from '@/pages/index/home.vue'

export default {
  data() {
    return {
      // -------------------------------------------home-------------------------------------------
      noticeType: 'warning',
      notices: [],
      categories: [],
      sysCategories: [],
      permissionCategories: [],
      recordCategories: [],
      // -------------------------------------------index-------------------------------------------
      PageCur: 'home',
      show: false,
      newUrl: null,
      newToast: null,
      content: '版本太低，请更新APP后再进行使用！',
      showTitle: false,
      confirmText: '去更新',
      newVersion: false,
      topBackGroupImageIndex: null,
      version: uni.getAppBaseInfo().appVersion,
      unShow: false,
      unNewUrl: null,
      remark: null,
      weatherShow: false,
      weatherRemark: null,
      weatherWarningUrl: null,
      scrollCategories: false, // 是否支持滑动模块 默认不支持
      picName: '活力春天',
      inter: [
        {
          title: 'mimicry',
          name: '活力春天',
          color: ''
        },
        {
          title: 'theme',
          name: '清爽夏日',
          color: ''
        },
        {
          title: 'theme',
          name: '金秋之韵',
          color: ''
        },
        {
          title: 'theme',
          name: '冬日之阳',
          color: ''
        },
        {
          title: 'theme',
          name: '幽兰星空',
          color: ''
        },
        {
          title: 'theme',
          name: '流星之夜',
          color: ''
        }
      ]
    }
  },
  components: {
    Home
  },
  async onLoad() {
    // 开启通知权限
    // this.$u.utils.permissions();
    // 初始化首页模块数据
    this.noticeType = 'warning'
    this.notices = []
    this.categories = []
    this.sysCategories = []
    this.permissionCategories = []
    this.recordCategories = []
    this.weatherShow = false
    this.weatherRemark = null
    this.weatherWarningUrl = null
  },
  async onShow() {
    this.getCount()
    this.getThemeIndex()
  },
  methods: {
    // -------------------------------------------home-------------------------------------------
    /**
     * 获取首页模块数据
     */
    async getHomeModules() {
      if (
        this.categories.length === 0 &&
        this.sysCategories.length === 0 &&
        this.permissionCategories.length === 0 &&
        this.recordCategories.length === 0
      ) {
        this.scrollCategories = false
        let categoriesCount = 0 // 合集个数
        let categoriesLength = 0 // 大于8个模块的合集个数
        this.$u.api.getHomeModules().then((resp) => {
          let data = JSON.parse(JSON.stringify(resp))
          data.forEach((item) => {
            if (item.type === 2) {
              this.categories = JSON.parse(JSON.stringify(item.data))
              if (this.categories.length !== 0) categoriesCount += 1
              if (this.categories.length > 8) categoriesLength += 1
            } else if (item.type === 3) {
              this.permissionCategories = JSON.parse(JSON.stringify(item.data))
              if (this.permissionCategories.length !== 0) categoriesCount += 1
              if (this.permissionCategories.length > 8) categoriesLength += 1
            } else if (item.type === 4) {
              this.recordCategories = JSON.parse(JSON.stringify(item.data))
              if (this.recordCategories.length !== 0) categoriesCount += 1
              if (this.recordCategories.length > 8) categoriesLength += 1
            } else if (item.type === 5) {
              this.sysCategories = JSON.parse(JSON.stringify(item.data))
              if (this.sysCategories.length !== 0) categoriesCount += 1
              if (this.sysCategories.length > 8) categoriesLength += 1
            }
          })
        })
      }
    },
    /**
     * 获取全局通告栏消息
     */
    async getNoticeInfo() {
      this.$u.api.getNoticeInfo().then((resp) => {
        if (resp) {
          this.noticeType = resp.type
          this.notices = resp.infos
        }
      })
    },
    async getCount() {
      // 获取未读消息数量
      let count = await this.$u.api.unReadMessageCount()

      // 处理token过期 重新获取正确的信息
      if (count.code == 401) {
        // 延迟1s查询
        await new Promise((res) => setTimeout(res, 1000))
        count = await this.$u.api.unReadMessageCount()
      }
      await this.getNoticeInfo()
      // 加载首页模块
      await this.getHomeModules()

      // 延迟1s查询
      await new Promise((res) => setTimeout(res, 500))
      // 赋值
      if (count == 0) {
        this.$set(this.categories[6], `count`, 0)
        this.$set(this.categories[6], `badge`, 0)
      } else {
        this.$set(this.categories[6], `count`, 1)
        this.$set(this.categories[6], `badge`, count)
      }
    },

    // -------------------------------------------index-------------------------------------------
    NavChange: function (e) {
      this.PageCur = e.currentTarget.dataset.cur
      // 当点击是 我的页面
      if (this.PageCur === 'mine') {
        if (!this.$u.utils.isLogin()) return
      }
    },
    async getThemeIndex() {
      const index = await this.$u.api.getThemeIndex()
      this.topBackGroupImageIndex = index
      this.picName = this.inter[index].name
    },
  }
}
</script>

<style lang="scss" scoped></style>
